package com.bus.admin.serviceImpl;

import cn.hutool.core.util.RandomUtil;
import com.bus.admin.service.BusWxPayService;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @项目名称: bus-admin
 * @包名称: com.bus.admin.serviceImpl
 * @创建人员: Mr.Administrator  张子艺
 * @创建时间: 2020-04-21 09:49
 * @版本:
 */

@Service
public class BusWxPayServiceImpl implements BusWxPayService {
    @Autowired
    WxPayService wxPayService;
    @Value("${wx.pay.RETURN_URL}")
    String RETURN_URL;

    @Override
    public String createOrder(String moeny) {
        WxPayUnifiedOrderRequest param = new WxPayUnifiedOrderRequest();
        param.setBody("请付款");
        param.setOutTradeNo(RandomUtil.randomNumbers(30));
        BigDecimal bigDecimal = new BigDecimal(moeny);
        param.setTotalFee(bigDecimal.multiply(new BigDecimal(100)).intValue());
        param.setSpbillCreateIp("123.12.12.123");
        param.setNotifyUrl(RETURN_URL);
        param.setTradeType("APP");
        try {
            return  wxPayService.createOrder(param) ;
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public WxPayUnifiedOrderResult createH5Order(String moeny) {
        WxPayUnifiedOrderRequest param = new WxPayUnifiedOrderRequest();
        param.setBody("请付款");
        param.setOutTradeNo(RandomUtil.randomNumbers(30));
        BigDecimal bigDecimal = new BigDecimal(moeny);
        param.setTotalFee(bigDecimal.multiply(new BigDecimal(100)).intValue());
        param.setSpbillCreateIp("123.12.12.123");
        param.setNotifyUrl(RETURN_URL);
        param.setTradeType("MWEB");
        try {

            return     wxPayService.unifiedOrder(param) ;
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return null;
    }
}
